﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Management;
using System.Text;
using Microsoft.HomeServer.SDK.Interop.v1;
using WhsFacebook.Net.Contracts;

namespace WhsFacebook.Whs.Service.Submitters
{
    public sealed class SystemInfoSubmitter : InfoSubmitter<WhsSystemInfo>
    {

        #region instance

        public static SystemInfoSubmitter Instance
        {
            get; private set;
        }

        static SystemInfoSubmitter()
        {
            Instance = new SystemInfoSubmitter();
        }

        #endregion

        private readonly WHSInfoClass _whsInfo;

        protected override WhsSystemInfo DoSubmitInternal()
        {
            var si = CreateForCurrentSystem();

            // submit
            using (var wc = new WhsFacebookServiceClientFactory())
            {
                wc.CreateChannel().UpdateSystemInfo(si);
            }

            // finalize
            return si;
        }

        /// <summary>
        /// see http://technet.microsoft.com/en-us/library/ee156538.aspx
        /// </summary>
        private WhsSystemInfo CreateForCurrentSystem()
        {
            var si = new WhsSystemInfo();

            // query information about OS
            try
            {
                var os = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem").Get().GetEnumerator();

                if (os.MoveNext())
                {
                    si.Locale = Convert.ToInt32(os.Current["Locale"]);
                    si.OsVersion = Convert.ToString(os.Current["Version"]);
                }
            }
            catch (Exception x)
            {
                if (_log.IsWarnEnabled)
                    _log.Warn("Failed to retrieve OS information", x);
            }

            // query information about machine
            try
            {
                var os = new ManagementObjectSearcher("SELECT * FROM Win32_ComputerSystem").Get().GetEnumerator();

                if (os.MoveNext())
                {
                    si.SystemManufacturer = Convert.ToString(os.Current["Manufacturer"]);
                    si.SystemModel = Convert.ToString(os.Current["Model"]);
                    si.TimeZone = Convert.ToInt32(os.Current["CurrentTimeZone"]);
                    si.TotalRam = Convert.ToInt64(os.Current["TotalPhysicalMemory"]);
                }
            }
            catch (Exception x)
            {
                if (_log.IsWarnEnabled)
                    _log.Warn("Failed to retrieve machine information", x);
            }

            // finalize
            si.InstanceId = WhsFacebookRegistry.GetInstanceId();
            si.WhsDomain = _whsInfo.GetDomainName();
            si.WhsVersion = FileVersionInfo.GetVersionInfo(
                Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles),
                @"Windows Home Server\qsm.exe")
                ).FileVersion;

            return si;
        }


        private SystemInfoSubmitter()
        {
            // initialize whs info object
            _whsInfo = new WHSInfoClass();
            _whsInfo.Init(Whs.Properties.Constants.ApplicationName);
        }

    }
}
